// reducer.js
// reducer 是一个纯函数，接收两个参数，第一个是初始状态，第二个是 action 对象
import {

    ADD,
    DELETE,
    EDIT,
    FIND
} from "./actionType";

// 这里我们导出了一个名为 counter 的函数，这个就是一个纯函数，也是 redux 里面称之为的 reducer
// 函数接收两个值，初始状态，action 对象

let stateData = {
    token: "",
    student: [{
        id: 0,
        name: "张三",
        age: 19,
        gender: "男",
        height: 175,
        phone: "15775692212"
    }, {
        id: 1,
        name: "李四",
        age: 20,
        gender: "男",
        height: 170,
        phone: "15775692214"
    }, {
        id: 2,
        name: "王五",
        age: 22,
        gender: "男",
        height: 172,
        phone: "15775692215"
    }],
    findStudent:[],

}
export default function counter(state = stateData, action) {

    if (action.type === ADD) {
        // const newState = state;

        // newState.student.push(action.data);
        return {
            token: state.token,
            student: [...state.student, action.data]
        };
    } else if (action.type === DELETE) {
        // 删除一条数据
        let arr = [...state.student];
        arr.splice(action.data.id, 1);

        return {
            student: arr
        }
    } else if (action.type === FIND) {
      
        state.findStudent=[action.data]
      
    } else {
        return state
    }
}